<template>
  <BasicModal
    v-bind="$attrs"
    @register="register"
    :title="displayModuleTitle"
    @ok="onOkButtonClick"
  >
    <BasicForm @register="registerForm"></BasicForm>
  </BasicModal>
</template>
<script lang="ts">
  import { computed, defineComponent, ref, unref } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { displayTitle } from '/@/utils/QcUnifyUtil';
  import { OperationTypeEnum } from '/@/enums/appEnum';
  import { QcUnifyTransToChildProps } from '/#/QcUnify';
  import { addIpBlackListFormSchema } from './blacklist.data';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { addBlackList, refreshBlackList } from '/@/api/platform/blackList';
  const { createMessage } = useMessage();
  export default defineComponent({
    components: { BasicModal, BasicForm },
    name: 'AddBlackList',
    emits: ['success'],
    setup(_, { emit }) {
      const operateType = ref(OperationTypeEnum.UN_KNOWN);
      const [registerForm, { setFieldsValue, resetFields, validate, updateSchema }] = useForm({
        labelWidth: 100,
        schemas: addIpBlackListFormSchema,
        showActionButtonGroup: false,
        actionColOptions: {
          span: 24,
        },
      });
      const qcUnifyProps = ref<QcUnifyTransToChildProps>();
      const dataId = ref();
      const [register, { closeModal, setModalProps }] = useModalInner((data) => {
        resetFields();
        if (data != undefined && data != null) {
          let unrefData = unref(data);
          qcUnifyProps.value = unrefData;
          //接收父组件传递的数据
          if (unrefData?.operateType) {
            //判断是否传入op属性
            operateType.value = unrefData.operateType;
          }
          if (unrefData?.ip || unrefData?.url) {
            setFieldsValue({
              ip: unrefData.ip,
              url: unrefData.url,
            });
          }
        }
      });
      async function onOkButtonClick() {
        let values = await validate();
        if (!values.ip && !values.url) {
          return createMessage.error('IP地址和URL至少有1个不为空，请输入');
        }
        setModalProps({ confirmLoading: true });
        addBlackList(values).then((res) => {
          if (!res) {
            createMessage.success('添加成功');

            refreshBlackList().then((res) => {
              if (!res) {
                createMessage.success('刷新黑名单成功');
                emit('success');
              } else {
                createMessage.error(res);
              }
            });
            closeModal();
          } else {
            createMessage.error('添加失败, 请稍后重试');
          }
          setModalProps({ confirmLoading: false });
        });
      }

      let displayModuleTitle = computed(() => {
        return displayTitle(qcUnifyProps.value);
      });
      return {
        register,
        registerForm,
        closeModal,
        displayModuleTitle,
        onOkButtonClick,
      };
    },
  });
</script>
